package com.skt.casdemo.widget;

import java.util.Random;

import com.skt.casdemo.R;
import com.skt.casdemo.service.UpdateWidgetService;
import com.skt.casdemo.service.UpdateWidgetService2;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class MyWidgetProvider2 extends AppWidgetProvider {
	
	public static String CLICK_ACTION = "com.skt.casdemo.CLICK";
    public static String REFRESH_ACTION = "com.skt.casdemo.viewDetail";
    public static String EXTRA_CITY_ID = "com.skt.casdemo.viewMessage";
	
	private static final String LOG = "com.skt.casdemo";
	
	
	

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.w(LOG, "onReceive method called");
		super.onReceive(context, intent);
	}




	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.w(LOG, "onUpdate method called");
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		// Get all ids
		ComponentName thiswidget = new ComponentName(context, MyWidgetProvider2.class);
		int[] allwidgetIds = appWidgetManager.getAppWidgetIds(thiswidget);
		
		// 1) process in other background service
		Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService2.class);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allwidgetIds);
		context.startService(intent);
		
		// 2) process in this method
		/*
		for (int widgetId : appWidgetIds){
			// Create some random data
			int number = new Random().nextInt(100);
			
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
			Log.d("Context Aware", String.valueOf(number));
			
			// Set the text
			remoteViews.setTextViewText(R.id.update, String.valueOf(number));
			
			// Register an onClickListener
			Intent intent = new Intent(context, MyWidgetProvider.class);
			intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
			
			PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
		*/
	}
	
}
